<?php
/***
 * 描述：商品管理[管理员权限]
 * @category		Admin		
 * @package			Action			
 * @subpackage			
 * @author			wanght <whtaok@gmail.com>
 * @since			2013-4-6
 * @version			$Id: ProductManageAction.class.php 42 2013-10-30 03:34:30Z whtaok@gmail.com $
 */
class ProductManageAction extends AdminAction {
	
	/**
	 *
	 * @access		protected
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	protected  function _initialize() {
		parent::_initialize();
		F('publiccate_list',null);
		$modWhere = array(
				'type'=>'商品单位',
				'is_valid'=>'2'
			);
		$this->assign('productunit_list',publiccate_list($modWhere));
		$this->usingModel="ProductLib";
	}
	
	/**
	 * 商品基本库查询
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function indexAct() {
		$search = array(
				'cate_code'=> I('cate_code'),
				'name'=>I('name'),
				'barcode'=>I('barcode'),
		);
		$mod=new ProductLibModel();
		list($list,$page) = $mod->findListPage($search);
		$this->assign('list',$list);
		$this->assign('page',$page);
		$this->assign('search',$search);
		$this->assign('cateTree',productcate_tree());
		$this->display('index');
	}
	
	/**
	 * 显示批量添加页面
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function showAddBatchAct($add_cnt=10) {
		$search = array(
			'cate_code'=>I('cate_code'),
		);
		for ($i=0 ; $i<$add_cnt ; $i++){
			$list[] = array(
				'ordlist'=>$i+1,
			);
		}
		$this->assign('list',$list);
		$this->assign('search',$search);
		$this->assign('cateTree',productcate_tree());
		$this->display('add_batch');
	}
	
	/**
	 * 批量添加操作
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function doAddBatchAct() {
		$mod=new ProductLibModel();
		$dataList = array();
		$cateCode = I('cate_code');
		$brand = I('brand');
		$name = I('name');
		$spec = I('spec');
		$spec_unit = I('spec_unit');
		$price_buy = I('price_buy');
		$price_sale = I('price_sale');
		$price_vip = I('price_vip');
		$ordlist = I('ordlist');
		$cateCode1 = get_productcate_code1($cateCode);
		$cateCode2 = $cateCode;
		if(!is_productcate_code2($cateCode2)){
			$this->error('商品分类选择不正确');
		}
		foreach ($name as $key=>$value){
			if($value){
				$item = array(
						'cate_code1' =>$cateCode1,
						'cate_code2' =>$cateCode2,
						'barcode'=>uniqid(),
						'brand'=>$brand[$key],
						'name'=>$name[$key],
						'spec'=>$spec[$key],
						'spec_unit'=>$spec_unit[$key],
						'price_buy'=>$price_buy[$key],
						'price_sale'=>$price_sale[$key],
						'price_vip'=>$price_vip[$key],
						'ordlist'=>$ordlist[$key],
						'batch_line'=>$key+1,
				);
				if(!$price_buy[$key]){
					$item['price_buy'] = $price_sale[$key];
				}
				if(!$price_vip[$key]){
					$item['price_vip'] = $price_sale[$key];
				}
				$data = $mod->create($item);
				if($mod->getError()){
					$this->error("第【{$item['batch_line']}】行数据：".$mod->getError());
				}else{
					$dataList[]=$data;
				}
			}
		}
		
		if($mod->addAll($dataList)){
			$mod=new ProductLibModel();
			$this->success('添加成功', U('index'));
		}else{
			$this->error('添加失败，请重新尝试');
		}
		
	}
	
	/**
	 * 显示批量编辑页面
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function showUpdBatchAct() {
		$cateCode = I('cate_code');
		$id = I('id');
		$mod=new ProductLibModel();
		if($cateCode){
			if(is_productcate_code2($cateCode)){
				$modWhere = array('cate_code2'=>$cateCode);
				$search['cate_code'] = $cateCode;
			}else{
				$modWhere = array('cate_code1'=>$cateCode);
			}
			$list = $mod->where($modWhere)->select();
		}else if($id){
			if(is_array($id)){
				$id = array_values($id);
				$modWhere = array('id'=>array('in',$id));
			}else{
				$modWhere = array('id'=>$id);
			}
			$list = $mod->where($modWhere)->select();
		}else{
			$this->error('未选定要编辑的信息');
		}
		$this->assign('search',$search);
		$this->assign("list",$list);
		$this->assign('cateTree',productcate_tree());
		$this->display('upd_batch');
	}
	
	/**
	 * 编辑操作
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function doUpdBatchAct() {
		$mod=new ProductLibModel();
		$dataList = array();
		$cateCode = I('cate_code');
		$choosedId = I('choosedId');	//选择更新的ID
		$id = I('id');					//全部ID
		$brand = I('brand');
		$name = I('name');
		$spec = I('spec');
		$spec_unit = I('spec_unit');
		$price_buy = I('price_buy');
		$price_sale = I('price_sale');
		$price_vip = I('price_vip');
		$ordlist = I('ordlist');
		$cateCode1 = get_productcate_code1($cateCode);
		$cateCode2 = $cateCode;
		foreach ($id as $key=>$value){
			if(in_array($value, $choosedId)){
				$item = array(
						'id' =>$id[$key],
						'brand'=>$brand[$key],
						'name'=>$name[$key],
						'spec'=>$spec[$key],
						'spec_unit'=>$spec_unit[$key],
						'price_buy'=>$price_buy[$key],
						'price_sale'=>$price_sale[$key],
						'price_vip'=>$price_vip[$key],
						'ordlist'=>$ordlist[$key],
						'batch_line'=>$key+1,
				);
				if(!$price_buy[$key]){
					$item['price_buy'] = $price_sale[$key];
				}
				if(!$price_vip[$key]){
					$item['price_vip'] = $price_sale[$key];
				}
				//如果选择了分类，则更新分类，否则不更新分类
				if(is_productcate_code2($cateCode2)){
					$item['cate_code1']=$cateCode1;
					$item['cate_code2']=$cateCode2;
				}
				$data = $mod->create($item);
				if($mod->getError()){
					$this->error("第【{$item['batch_line']}】行数据：".$mod->getError());
				}else{
					$dataList[]=$data;
				}
			}
		}
		foreach ($dataList as $key=>$value){
			$flag = $mod->save($value);
			if($flag===false){
				$msg .= $value['name'].',';
			}
		}
		if(!$msg){
			$this->success('保存成功', U('index'));
		}else{
			$this->error('保存失败，请重新尝试');
		}
	}
	/**
	 * 编辑打包商品页面
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function showEditGroupAct($id=0){
		$mod=new ProductLibModel();
		$detail = $mod->find($id);
		if($detail['spec_qty']==1){
			$listGroup = $mod->setModWhere(array('stock_prod_id'=>$id))->select();
			$this->assign('detail',$detail);
			$this->assign('listGroup',$listGroup);
			$this->display('edit_group');
		}else{
			$this->error('该商品不可以进行打包');
		}
	}
	
	/**
	 * 添加打包商品页面
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function doEditGroupAct(){
		$id = I('id');
		$mod=new ProductLibModel();
		if($id){
			$flag = $mod->updOne();
			if($flag !== false){
				$this->success('修改成功',U('showEditGroup',array('id'=>I('stock_prod_id'))),1);
			}else{
				$this->error($mod->getError());
			}
		}else{
			$lastId = $mod->addOne();
			if($lastId){
				$this->redirect('showEditGroup',array('id'=>I('stock_prod_id')));
			}else{
				$this->error('打包商品添加失败');
			}
		}
	}
	
	/**
	 * 删除操作
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function doDelAct(){
		$id_list = array_values(I('id'));
		if($id_list){
			$mod = new ProductLibModel();
			$data['id'] = array('in',$id_list);
			$mod->where($data)->delete();
		}
		$this->success('删除成功', U('index'));
	}
	
	/**
	 * 描述：上传图片
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-4-23
	 */
	public function doUploadAct() {
		$id = I('id');
		//访问路径 http：//
		$pathUrl = photo_url_path('/product/');
		//上传路径 file：///
		$pathReal = photo_real_path('/product/',true);
		$myconfig = array(
				'allowExts'         =>  array('jpg'),   // 允许上传的文件后缀 留空不作后缀检查
				'saveRule'          =>  $id,			// 上传文件命名规则
				'uploadReplace'     =>  true,// 存在同名是否覆盖
				'thumbMaxWidth'     =>  '160',// 缩略图最大宽度
				'thumbMaxHeight'    =>  '160',// 缩略图最大高度
			);
		$upload = $this->doUpload($pathReal,$myconfig);
		if($upload->getErrorMsg()){
			$rtn_data = array('error'=>0,'message'=>$upload->getErrorMsg());
		}else{
			$uploadFiles = $upload->getUploadFileInfo();
			$rtn_data = array(
					'error'=>0,
					'id'=>$id,
					'url'=>$pathUrl.$id.'.png'
			);
	
		}
		header('Content-Type:text/html; charset=utf-8');
		//header('Content-Type:application/json; charset=utf-8');
        exit(json_encode($rtn_data));
	}


}

?>